package com.wwj.core.wawa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wwj.core.wawa.entity.WaWaType;
import com.wwj.core.wawa.entity.mapper.WaWaTypeMapper;
import com.wwj.core.wawa.service.IRoomService;
import com.wwj.core.wawa.service.IWaWaTypeService;
import com.wwj.framework.utils.ReturnData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/3.
 */
@Service
public class WaWaTypeServiceImpl implements IWaWaTypeService {

    @Resource
    private WaWaTypeMapper waWaTypeMapper;

    @Resource
    private IRoomService roomService;

    @Override
    public WaWaType findById(Integer id) {
        return waWaTypeMapper.selectWaWaTypeById(id);
    }

    @Override
    public List<WaWaType> findAll() {
        return waWaTypeMapper.selectAll();
    }

    @Override
    public Page<WaWaType> findByPage(Map<String, Object> params, int offset, int limit) {
        PageHelper.offsetPage(offset,limit);
        return (Page)waWaTypeMapper.selectByPage(params);
    }

    @Override
    public void update(WaWaType waWaType) {
        waWaTypeMapper.updateWaWaType(waWaType);
    }

    @Override
    public void add(WaWaType waWaType) {
        waWaTypeMapper.addWaWaType(waWaType);
    }

    @Override
    public ReturnData deleteById(Integer id) {
        if(null == id) return new ReturnData().failure("id不能为空");
        int roomNum = roomService.findByWaWaTypeId(id);
        if(roomNum > 0){
            return new ReturnData().failure("类型捆绑了房间，请将该娃娃类型房间修改或删除");
        }else{
            waWaTypeMapper.deleteById(id);
            return new ReturnData().success("删除成功");
        }
    }
}
